﻿using System.Collections.Generic;
using EzDesk.Core.Model;
using EzDesk.DesktopClient.Event;
using System.Linq;
using EzDesk.DesktopClient.Storage;

namespace EzDesk.DesktopClient
{
    public class AppManagement : ManagementBaseWithUserLogStatus<AppManagement>
    {
        private List<AppFunction> _appFunctionCollection;

        private const string FastNavigationString = "上班考勤_下班考勤_写新信件";

        public List<AppFunctionType> FunctionTypeCollection
        {
            get;
            private set;
        }

        public List<AppFunction> ShortCutFunctionCollection { get; private set; }

        public List<AppFunction> GetFunction(AppFunctionType functionType = null, AppFunctionType functionCategory = null)
        {
            IEnumerable<AppFunction> result = _appFunctionCollection;
            if (functionType != null && functionType.TypeName != "全部")
                result = result.Where(af => af.Type.Id == functionType.Id);

            if (functionCategory != null)
                result = result.Where(af => af.Category.Id == functionCategory.Id);
            return result.OrderBy(f => f.Privilege).ToList();
        }

        public List<AppFunctionType> GetFunctionCategory(AppFunctionType functionType)
        {
            if (functionType.TypeName == "全部")
                return _appFunctionCollection
                        .Select(af => af.Category)
                        .GroupBy(c => c.Id)
                        .Select(gc => gc.First())
                        .OrderBy(fc => fc.Privilege)
                        .ToList();

            return _appFunctionCollection
                    .Where(af => af.Type.Id == functionType.Id)
                    .Select(af => af.Category)
                    .GroupBy(c => c.Id)
                    .Select(gc => gc.First())
                    .OrderBy(fc => fc.Privilege)
                    .ToList();
        }

        public void UpdateShortcutOrder(IList<AppFunction> appFunctions)
        {
            string appFunctionGuids = string.Join(",", appFunctions.Select(af => af.Id));
            CoreClientService.UpdateAppFunctionShortcutSequnce(CurrentUser.Id, appFunctionGuids);
        }

        protected override void OnUserLogin(LoginArgs args)
        {
            base.OnUserLogin(args);

            ClientDbStorage.GetDataContext(CurrentUser.UserCode);
            _appFunctionCollection = new List<AppFunction>(CoreClientService.GetAllAppFunctions(CurrentUser.Id));

            FunctionTypeCollection = _appFunctionCollection
                .Select(af => af.Type)
                .GroupBy(t => t.Id)
                .Select(gt => gt.First())
                .OrderBy(ft => ft.Privilege)
                .ToList();

            FunctionTypeCollection.Insert(0, new AppFunctionType { TypeName = "全部", DisplayText = "全部", Privilege = -1 });

            ShortCutFunctionCollection =
                _appFunctionCollection
                    .Where(af => FastNavigationString.Contains(af.Title))
                    .ToList();
        }

        protected override void AfterUserLogin(LoginArgs args)
        {

        }

        protected override void OnUserLogout(LogoutArgs args)
        {
            base.OnUserLogout(args);

            if (_appFunctionCollection != null) _appFunctionCollection.Clear();

            FunctionTypeCollection = null;
        }
    }
}
